package com.itguigu.rabbitmq.three;

import com.itguigu.rabbitmq.utils.RabbitMqutil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @Date 2021/7/18 13:30
 * @Created by wangle
 * @Description 消息在手动应答时不丢失，放回对列中重新消费
 */
public class Task2 {
    public static String task_queue_name = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqutil.getChannel();
        //开启发布确认
        channel.confirmSelect();
        boolean durable = true;

        //声明对列
        channel.queueDeclare(task_queue_name, durable, false, false, null);

        //从控制台输入信息
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {


            String message = scanner.next();
            //设置生产者发送消息保存为持久化消息(保存到磁盘上) 否则会保存到内存中

            channel.basicPublish("", task_queue_name, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发出消息：" + message);


        }
    }


}
